home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 114 / CDRom114.iso / internet / extens / tcopy / Copy Plain Text.xpi / chrome / copyplaintext.jar / content / copyplaintext.js < prev    next >
Encoding:
Text File  |  2004-09-09  |  2.2 KB  |  72 lines

  1.  
  2. function copyplaintext(){
  3.     try{
  4.         // Get String
  5.         var str = copyplaintext_getSelection() + "";
  6.         
  7.         // Format
  8.             var oPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
  9.             
  10.             // Trim
  11.             if(oPrefs.prefHasUserValue("copyplaintext.formatting.trim") && oPrefs.getBoolPref("copyplaintext.formatting.trim"))
  12.                 str = str.replace(/^\s*|\s*$/g, "");
  13.             
  14.             // Extra Lines
  15.             if(oPrefs.prefHasUserValue("copyplaintext.formatting.extra.newline") && oPrefs.getBoolPref("copyplaintext.formatting.extra.newline"))
  16.                 str = str.replace(/[\n\r]+/g, "\n");
  17.             
  18.             // Extra Space
  19.             if(oPrefs.prefHasUserValue("copyplaintext.formatting.extra.space") && oPrefs.getBoolPref("copyplaintext.formatting.extra.space"))
  20.                 str = str.replace(/[ \t]+/g, " ");
  21.         
  22.         
  23.         // Copy
  24.         if(str != null && str.length > 0){
  25.             var oClipBoard = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
  26.                 oClipBoard.copyString(str);
  27.         }
  28.                 
  29.     }catch(err) { alert("An unknown error occurred\n"+ err) }
  30. }
  31.  
  32. function copyplaintext_init(){
  33.     var oContext = document.getElementById('contentAreaContextMenu');
  34.     
  35.     if(oContext){
  36.         oContext.setAttribute("onpopupshowing", "copyplaintext_showContextMenu(); "+ oContext.getAttribute("onpopupshowing"));
  37.     }
  38. }
  39.  
  40. function copyplaintext_showMenu(){
  41.  
  42.     // Is Text Select
  43.     try{        
  44.         var str = copyplaintext_getSelection();
  45.         if(str != null && str.length > 0) {
  46.             return true;
  47.         }
  48.                 
  49.     }catch(err) { }
  50.     
  51.     return false;
  52. }
  53.  
  54. function copyplaintext_getSelection() {
  55.     var focusedWindow = document.commandDispatcher.focusedWindow;
  56.     var searchStr = focusedWindow.__proto__.getSelection.call(focusedWindow);
  57.     searchStr = searchStr.toString();
  58.     
  59.     return searchStr;
  60. }
  61.  
  62. function copyplaintext_showEditMenu(){
  63.     document.getElementById('copyplaintext-edit-menu').setAttribute("disabled", !copyplaintext_showMenu());
  64. }
  65.  
  66. function copyplaintext_showMozEditMenu(){
  67.     document.getElementById('copyplaintext-moz-edit-menu').setAttribute("disabled", !copyplaintext_showMenu());
  68. }
  69.  
  70. function copyplaintext_showContextMenu(){
  71.     document.getElementById('copyplaintext-context-menu').setAttribute("collapsed", !copyplaintext_showMenu());
  72. }